/*
  Copyright 2012 Sergey Ostanin

  Licensed under the Apache License, Version 2.0 (the "License");
  you may not use this file except in compliance with the License.
  You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
*/

package ru.irenproject.orderQuestion;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import org.apache.commons.lang3.Validate;

import ru.irenproject.FormulationSupplier;
import ru.irenproject.Question;
import ru.irenproject.TestCheck;
import ru.irenproject.pad.Pad;
import ru.irenproject.store.AbstractThing;
import ru.irenproject.store.Event;
import ru.irenproject.store.Stash;

import com.google.protobuf.ByteString;

public final class OrderQuestion extends AbstractThing
    implements Question, FormulationSupplier {
  public enum ChangeEvent implements Event { INSTANCE }

  public static final int MAX_ELEMENTS = 50;

  @Stash private OrderQuestionProtos.OrderQuestion.Builder $;

  @Inject OrderQuestion() {}

  @Override protected void initialize() {
    Pad formulation = store().create(Pad.class);
    $.setFormulation(formulation.id());
  }

  @Override public Pad formulation() {
    return get($.getFormulation(), Pad.class);
  }

  public List<Pad> itemList() {
    return store().getBatch($.getItemList(), Pad.class);
  }

  public List<Pad> distractorList() {
    return store().getBatch($.getDistractorList(), Pad.class);
  }

  public int itemCount() {
    return $.getItemCount();
  }

  public int distractorCount() {
    return $.getDistractorCount();
  }

  public Pad addItem() {
    TestCheck.input(itemCount() < MAX_ELEMENTS);

    Pad res = store().create(Pad.class);
    $.addItem(res.id());
    post(ChangeEvent.INSTANCE);

    return res;
  }

  public Pad addDistractor() {
    TestCheck.input(distractorCount() < MAX_ELEMENTS);

    Pad res = store().create(Pad.class);
    $.addDistractor(res.id());
    post(ChangeEvent.INSTANCE);

    return res;
  }

  public void deleteItem(int index) {
    checkItemIndex(index);

    List<ByteString> ids = new ArrayList<>($.getItemList());
    ids.remove(index);
    $.clearItem().addAllItem(ids);
    post(ChangeEvent.INSTANCE);
  }

  private void checkItemIndex(int index) {
    TestCheck.input(index >= 0);
    TestCheck.input(index < itemCount());
  }

  public void deleteDistractor(int index) {
    checkDistractorIndex(index);

    List<ByteString> ids = new ArrayList<>($.getDistractorList());
    ids.remove(index);
    $.clearDistractor().addAllDistractor(ids);
    post(ChangeEvent.INSTANCE);
  }

  private void checkDistractorIndex(int index) {
    TestCheck.input(index >= 0);
    TestCheck.input(index < distractorCount());
  }

  public void moveItem(int index, boolean forward) {
    checkItemIndex(index);
    int newIndex = index + (forward ? 1 : -1);
    checkItemIndex(newIndex);

    ByteString temp = $.getItem(index);
    $.setItem(index, $.getItem(newIndex));
    $.setItem(newIndex, temp);
    post(ChangeEvent.INSTANCE);
  }

  public void moveDistractor(int index, boolean forward) {
    checkDistractorIndex(index);
    int newIndex = index + (forward ? 1 : -1);
    checkDistractorIndex(newIndex);

    ByteString temp = $.getDistractor(index);
    $.setDistractor(index, $.getDistractor(newIndex));
    $.setDistractor(newIndex, temp);
    post(ChangeEvent.INSTANCE);
  }

  public boolean hasItemLimit() {
    return $.hasItemLimit();
  }

  public int itemLimit() {
    Validate.isTrue(hasItemLimit());
    return $.getItemLimit();
  }

  public void setItemLimit(int value) {
    TestCheck.input(value >= 1);
    TestCheck.input(value <= MAX_ELEMENTS);
    $.setItemLimit(value);
    post(ChangeEvent.INSTANCE);
  }

  public void clearItemLimit() {
    $.clearItemLimit();
    post(ChangeEvent.INSTANCE);
  }

  public boolean hasDistractorLimit() {
    return $.hasDistractorLimit();
  }

  public int distractorLimit() {
    Validate.isTrue(hasDistractorLimit());
    return $.getDistractorLimit();
  }

  public void setDistractorLimit(int value) {
    TestCheck.input(value >= 1);
    TestCheck.input(value <= MAX_ELEMENTS);
    $.setDistractorLimit(value);
    post(ChangeEvent.INSTANCE);
  }

  public void clearDistractorLimit() {
    $.clearDistractorLimit();
    post(ChangeEvent.INSTANCE);
  }

  public boolean hasDefaultOptions() {
    return !hasItemLimit() && !hasDistractorLimit();
  }

  @Override public void setDefaults(String initialText) {
    formulation().appendText(initialText);
  }
}
